﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockBusiness;
using MockCommon;
using MockDataTransferObject;
using System.Globalization;

namespace Presentation
{
    public partial class DetailsContract : Page
    {
        private ContractBusiness _contractBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;
        private Label _contractTypeLabel;
        private Label _consultantLabel;
        private Label _clientLabel;
        private Label _contentLabel;
        private Label _startDateLabel;
        private Label _endDateLabel;
        private Label _firstNameLabel;
        private Label _lastNameLabel;
        private Label _birthDateLabel;
        private Label _genderLabel;
        private Label _telephone1Label;
        private Label _telephone2Label;
        private Label _sortCodeLabel;
        private Label _branchLabel;
        private Label _dayInWeekLabel;
        private Button _backButton;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _contractBusiness = ContractBusiness.RetrieveInstance();
                _warningLabel = (Label)Master.FindControl("WarningLabel");
                _errorLabel = (Label)Master.FindControl("ErrorLabel");
                _successLabel = (Label)Master.FindControl("SuccessLabel");
                _contractTypeLabel = (Label)ViewContractUserControl.FindControl("ContractTypeLabel");
                _consultantLabel = (Label)ViewContractUserControl.FindControl("ConsultantLabel");
                _clientLabel = (Label)ViewContractUserControl.FindControl("ClientLabel");
                _contentLabel = (Label)ViewContractUserControl.FindControl("ContentLabel");
                _startDateLabel = (Label)ViewContractUserControl.FindControl("StartDateLabel");
                _endDateLabel = (Label)ViewContractUserControl.FindControl("EndDateLabel");
                _firstNameLabel = (Label)ViewContractUserControl.FindControl("FirstNameLabel");
                _lastNameLabel = (Label)ViewContractUserControl.FindControl("LastNameLabel");
                _birthDateLabel = (Label)ViewContractUserControl.FindControl("BirthDateLabel");
                _genderLabel = (Label)ViewContractUserControl.FindControl("GenderLabel");
                _telephone1Label = (Label)ViewContractUserControl.FindControl("Telephone1Label");
                _telephone2Label = (Label)ViewContractUserControl.FindControl("Telephone2Label");
                _sortCodeLabel = (Label)ViewContractUserControl.FindControl("SortCodeLabel");
                _branchLabel = (Label)ViewContractUserControl.FindControl("BranchLabel");
                _dayInWeekLabel = (Label)ViewContractUserControl.FindControl("DayInWeekLabel");
                _backButton = (Button)ViewContractUserControl.FindControl("BackButton");
                _backButton.Click += new EventHandler(BackButton_Click);
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = (User)Session["user"];
                if (user.Role.RoleName == "Consultant")
                {
                    if (!IsPostBack)
                    {
                        Loading();
                    }
                }
                else
                {
                    Server.Transfer("NotAuthorized.aspx");
                }
            }
            else
            {
                Response.Redirect("LogOn.aspx");
            }
            if (IsPostBack)
            {
                _warningLabel.Text = "";
                _errorLabel.Text = "";
                _successLabel.Text = "";
            }
        }

        protected void BackButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewContracts.aspx");
        }

        public void Loading()
        {
            try
            {
                Guid contractId = Guid.Parse(Request.QueryString["cid"].ToString());
                MockDataTransferObject.Contract contract = _contractBusiness.GetById(contractId);
                _consultantLabel.Text = contract.Consultant.LastName;
                _contractTypeLabel.Text = contract.ContractType.ContractTypeName;
                _clientLabel.Text = contract.Client.LastName;
                _contentLabel.Text = contract.Content;
                _startDateLabel.Text = String.Format(CultureInfo.CurrentCulture, "{0:MM/dd/yyyy}", contract.StartDate);
                _endDateLabel.Text = String.Format(CultureInfo.CurrentCulture, "{0:MM/dd/yyyy}", contract.EndDate);

                _firstNameLabel.Text = contract.Contractor.FirstName;
                _lastNameLabel.Text = contract.Contractor.LastName;
                _birthDateLabel.Text = String.Format(CultureInfo.CurrentCulture, "{0:MM/dd/yyyy}", contract.Contractor.BirthDate);
                _genderLabel.Text = contract.Contractor.Gender ? "Male" : "Female";
                _telephone1Label.Text = contract.Contractor.Telephone1;
                _telephone2Label.Text = contract.Contractor.Telephone2;
                _sortCodeLabel.Text = contract.Contractor.SortCode;
                _branchLabel.Text = contract.Contractor.Branch;
                _dayInWeekLabel.Text = contract.Contractor.DayInWeek.ToString(CultureInfo.CurrentCulture);
            }
            catch (NullReferenceException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }
    }
}